const Koa = require('koa');

const app = new Koa();

const jwt = require('jsonwebtoken');

// const author = require('./middleware/author');

// app.use(author());

const query = require('./db/index');

let hmc = require('./util/hmc');

//处理post 传递过来参数
const bodyparser = require('koa-bodyparser');

app.use(bodyparser());

//处理路由，写接口

const Router = require('koa-router');

const router = new Router();

app.use(router.routes());

app.use(router.allowedMethods());

//写接口
//注册接口  post 向服务器提交数据，服务器会多数据   put  修改服务器的数据，不会增加数据
router.post('/api/registry',async (ctx,next) => {
    let {phone,password} = ctx.request.body;
    //容错处理  参数的校验
    if(password && /^\d{11}$/.test(phone)){
        //1.排重
        let target = await query('select * from userlist where phone=?',[phone]);
        if(target.length){
            //已经存在
            ctx.body = {
                code:3,
                msg:'此手机号已被使用'
            }
        }else{
            //添加
            try{
                let hmcPwd = hmc(password); //加密
                await query('insert into userlist (phone,password) values (?,?)',[phone,hmcPwd])
                ctx.body = {
                    code:1,
                    msg:'注册成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//登录接口  
router.post('/api/login',async (ctx,next) => {
    let {phone,password} = ctx.request.body;
    //容错处理
    if(password && /^\d{11}$/.test(phone)){
        let hmcPwd = hmc(password);
        //where  并且关系  and  或者 or
        let list = await query('select * from userlist where phone=? and password=?',[phone,hmcPwd])
        console.log(list);
        if(list.length){
            //生成token  jwt.sign(签进去的内容,密码,{过期时间:})
            let token = jwt.sign({uid:list[0].id},'lixd',{expiresIn:10});
            ctx.body = {
                code:1,
                msg:'登录成功',
                uid:list[0].id,
                token
            } 
        }else{
            ctx.body = {
                code:0,
                msg:'登录失败'
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//签到
router.post('/api/sign',async (ctx,next) => {
    let {uid} = ctx.request.body;
    //容错处理
    if(uid){
        let y = new Date().getFullYear();
        let m = new Date().getMonth() + 1;
        let d = new Date().getDate();
        let time = `${y}-${m}-${d}`;
        //不能重复签到哦 今天 你 是否签到？
        let sign = await query('select * from sign_list where time=? and uid=?',[time,uid]);
        if(sign.length){
            ctx.body = {
                code:3,
                msg:'今日已签到哦'
            }
        }else{
            //签到
            try{
                await query('insert into sign_list (time,uid) values (?,?)',[time,uid]);
                ctx.body = {
                    code:1,
                    msg:'签到成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

//签到的天数
router.get('/api/day',async (ctx,next) => {
    let {uid} = ctx.query;
    let count = await query('select count(*) from sign_list where uid=?',[uid]);
    ctx.body = {
        code:1,
        count:count[0]['count(*)']
    }
})


app.listen(3000,() => {
    console.log("服务启动成功")
})